from script.calculate_grade.calculate_grade1.nc_ioi_calculate_grade1 import nc_ioi_calculate_grade1
from script.get_contest_info.result.ioi_nowcoder import nc_ac_and_score
import pymongo
import time
from model.update_contest.nowcoder.use.sort_rule import nc_ioi_sort_rule
from model.update_contest.config.config import nc_ioi_once_time_sleep
"""





"""
# def nc_ac_and_time(contest_id , uid):

def nc_ioi_update_contest(contest_id):
    client = pymongo.MongoClient("mongodb://localhost:27017/")
    db = client["ACM_MEMBER"]
    collections = db["NOWCODER_INFORMATION"]
    contest_members = []
    for collection in collections.find():
        member = {}
        member["student_id"] = collection["student_id"]
        time.sleep(nc_ioi_once_time_sleep)
        member["ac_number"] , member["score"] = nc_ac_and_score(contest_id , collection["nowcoder_id"] )
        if member["score"] != None :
            contest_members.append(member)
    contest_members = nc_ioi_sort_rule(contest_members)
    contest_members = nc_ioi_calculate_grade1(contest_members)

    for member in contest_members:
        dict_by = {
            "nowcoder_contest" + str(contest_id): {
                "ac_number" : member["ac_number"],
                "score"      : member["score"],
                "grade1"     : member["grade1"]
            }
        }
        collections.update_one({"student_id": member["student_id"]}, {"$set": dict_by}, upsert=True)
    client.close()

if __name__ == '__main__':
    nc_ioi_update_contest(91283)